package com.pimwi.dp
{
  import com.pimwi.model.ModelLocator;
  import com.pimwi.vo.Contact;
  
  import flash.events.Event;
  import flash.events.EventDispatcher;
  
  import mx.collections.ArrayCollection;

  public class ContactDP extends EventDispatcher
  {
    private var _contact:Contact;

    public function ContactDP(contact:Contact = null)
    {
      _contact = contact;
      dispatchEvent(new Event("contactChanged"));
    }

    [Bindable("contactChanged")]
    public function get contact():Contact
    {
      return _contact;
    }
    public function set contact(value:Contact):void
    {
      _contact = value;
      dispatchEvent(new Event("contactChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("idChanged")]
    public function get id():int
    {
      return _contact.id;
    }
    public function set id(value:int):void
    {
      _contact.id = value;
      dispatchEvent(new Event("idChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("firstNameChanged")]
    public function get firstName():String
    {
      return _contact.firstName;
    }
    public function set firstName(value:String):void
    {
      _contact.firstName = value;
      dispatchEvent(new Event("firstNameChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("lastNameChanged")]
    public function get lastName():String
    {
      return _contact.lastName;
    }
    public function set lastName(value:String):void
    {
      _contact.lastName = value;
      dispatchEvent(new Event("lastNameChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("addressChanged")]
    public function get address():String
    {
      return _contact.address;
    }
    public function set address(value:String):void
    {
      _contact.address = value;
      dispatchEvent(new Event("addressChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("cityChanged")]
    public function get city():String
    {
      return _contact.city;
    }
    public function set city(value:String):void
    {
      _contact.city = value;
      dispatchEvent(new Event("cityChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("emailChanged")]
    public function get email():String
    {
      return _contact.email;
    }
    public function set email(value:String):void
    {
      _contact.email = value;
      dispatchEvent(new Event("emailChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("phoneChanged")]
    public function get phone():String
    {
      return _contact.phone;
    }
    public function set phone(value:String):void
    {
      _contact.phone = value;
      dispatchEvent(new Event("phoneChanged"));
    }

    [Bindable("contactChanged")]
    [Bindable("groupChanged")]
    public function get group():GroupDP
    {
      var userGroups:ArrayCollection = ModelLocator.getInstance().userGroups;
      for each (var _group:GroupDP in userGroups)
      {
        if (_group.id == contact.groupId)
        {
          return _group;
        }
      }
      return null;
    }
    [Bindable("contactChanged")]
    [Bindable("groupChanged")]
    public function get groupIndex():int
    {
      var userGroups:ArrayCollection = ModelLocator.getInstance().userGroups;
      var index:int = 0;
      for each (var _group:GroupDP in userGroups)
      {
        if (_group.id == contact.groupId)
        {
          return index;
        }
        index++;
      }
      return 0;
    }
    [Bindable("contactChanged")]
    [Bindable("groupChanged")]
    public function get groupName():String
    {
      if (contact.groupId == 0)
      {
        return "N/A";
      }
      var userGroups:ArrayCollection = ModelLocator.getInstance().userGroups;
      for each (var _group:GroupDP in userGroups)
      {
        if (_group.id == contact.groupId)
        {
          return _group.name;
        }
      }
      return "N/A";
    }
    public function set groupId(value:int):void
    {
      _contact.groupId = value;
      dispatchEvent(new Event("groupChanged"));
    }
  }
}